#include "lib.h"
#include "libglob.h"
#include "rmanager.h"
#include "rresource.h"

#include "fontclass.h"
#include "fontfactory.h"
#include "americancodepage.h"
#include "digitscodepage.h"
#include "europeancodepage.h"
#include "southafricancodepage.h"
#include "currencycodepage.h"

#include "systemfont.h"
using namespace Library;

//----------------------------------------------------------------------------
// Public, static
//

Font *
FontFactory::create(const String& name)
{
	Font *				font;
	Font::Resource *	resource;

	font = 0;

	if (name.isEmpty())
		return font;

	//it's an fnt file, open it from a resource
	if (name.find(".fnt") != String::NPOS)
	{
		resource = (Font::Resource *)gResourceManager->get(name);
		if (resource)
		{
			// REVISIT: This *should* check the font version too...
			switch (resource->fontClass)
			{
				case FONTCLASS_AMERICANCODEPAGE:
					font = new AmericanCodepage(name);
					break;

				case FONTCLASS_EUROPEANCODEPAGE:
					font = new EuropeanCodepage(name);
					break;

				case FONTCLASS_DIGITSCODEPAGE:
					font = new DigitsCodepage(name);
					break;

				case FONTCLASS_SOUTHAFRICANCODEPAGE:
					font = new SouthAfricanCodepage(name);
					break;

				case FONTCLASS_CURRENCYCODEPAGE:
					font = new CurrencyCodepage(name);
					break;

				default:
					font = 0;
					break;
			}

			Library::Resource::release(resource);
		}
		else
		{
				DEBUGMSG(("FontFactory::create(): %S not found in resource file.", (const wchar_t*)name));
		}
	}
	else
	{
		//font format: 
		//12;b;255;255;255;

		font = new SystemFont(name);

		if (font && font->isLoaded())
		{
			return font;
		}
		else
		{
			delete font;
			font = 0;
		}
	}

	return font;
}

